from rest_framework import serializers

from apps.article.serializers import ArticleListSerializer
from apps.questions.models import Label, Question, Reply
from apps.users.models import User


class QuestionSerializerCreat(serializers.ModelSerializer):
    class Meta:
        model = Question
        fields = '__all__'


class QuestionSerializerForList(serializers.ModelSerializer):

    user = serializers.StringRelatedField(read_only=True)
    labels = serializers.StringRelatedField(read_only=True, many=True)
    class Meta:
        model = Question
        fields = ('id', 'createtime', 'labels', 'solve', 'reply', 'replyname', 'replytime', "title", "unuseful_count", "useful_count", "user", "visits")

class ReplySerializerForCreate(serializers.ModelSerializer):

    class Meta:
        model = Reply
        fields = '__all__'

class AnswersQuestionSerializer(serializers.ModelSerializer):

    class Meta:
        model = Reply
        fields = '__all__'

class QusetionUsersSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'avatar')


class QuestionSubsSerializer(serializers.ModelSerializer):
    user = QusetionUsersSerializer(read_only=True)

    class Meta:
        model = Question
        fields = ('id', 'content', 'createtime', 'useful_count', 'unuseful_count', 'user')


class CommentsQuestionSerializer(serializers.ModelSerializer):
    subs = QuestionSubsSerializer(read_only=True, many=True)
    user = QusetionUsersSerializer(read_only=True)
    parent = QuestionSubsSerializer(read_only=True)
    class Meta:
        model = Reply
        fields = ["id", "content","createtime","useful_count",'problem',"unuseful_count","subs","user","parent"]



class QuestionSerializerForDetail(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    labels = serializers.StringRelatedField(read_only=True, many=True)
    # replies = CommentsQuestionSerializer(read_only=True, many=True)
    comment_question = CommentsQuestionSerializer(read_only=True, many=True)
    comment_reply = CommentsQuestionSerializer(read_only=True, many=True)
    answer_question = CommentsQuestionSerializer(read_only=True, many=True)

    class Meta:
        model = Question
        fields = ["id","createtime","labels","reply","replyname","replytime","title","unuseful_count","useful_count","user","visits","content","comment_question","comment_reply","answer_question"]


class LabelSerializer(serializers.ModelSerializer):
    # 标签序列化器类
    class Meta:
        model = Label
        fields = ('id', 'label_name')

class LabelSerializerWithQuestionAndArticle(serializers.ModelSerializer):
    questions = QuestionSerializerForList(many=True, read_only=True)
    articles = ArticleListSerializer(many=True, read_only=True)

    class Meta:
        model = Label
        fields = "__all__"

class LabelSerializerSmiple(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = "__all__"


class LabelFullSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = '__all__'

